package com.gxt.org.service.impl;

import com.gxt.basic.service.impl.BaseServiceImpl;
import com.gxt.org.domain.Department;
import com.gxt.org.mapper.DepartmentMapper;
import com.gxt.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;
    @Override
    public void insert(Department department) {
        department.setCreate_time(new Date());
        super.insert(department);
        //拼接path path=祖先id(通过查询该部门父部门的path)+自己的id
        //没有父部门path为自己的id
        Long id = department.getId();//自己的id
        String path = "";
        if (department.getParent().getId() == null){
            path = "/"+id;
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            path = parent.getPath()+"/"+id;
        }
        //修改department
        department.setPath(path);
        super.update(department);
    }

    @Override
    public void update(Department department) {
        department.setUpdate_time(new Date());
        String path = "";
        Long id = department.getId();//自己的id
        if (department.getParent().getId() == null){
            path = "/"+id;
        }else {
            Department parent = departmentMapper.loadById(department.getParent().getId());
            path = parent.getPath()+"/"+id;
        }
        //修改department
        department.setPath(path);
        super.update(department);
    }


    @Override
    public List<Department> getDepartmentTree() {
        return departmentMapper.getDepartmentTree();
    }


}
